package com.zhunaer.client;

import com.zhunaer.service.UserService;
import com.zhunaer.service.OrderService;
import com.zhunaer.service.RoomService;
import com.zhunaer.entity.User;
import java.util.Scanner;

/**
 * 酒店预订系统控制台客户端
 *
 * <p>该类提供用户交互界面，处理完整的酒店预订流程，包括：
 * <ul>
 *   <li>收集用户输入信息</li>
 *   <li>调用服务层完成业务逻辑</li>
 *   <li>处理并反馈操作结果</li>
 * </ul>
 */
import com.zhunaer.service.impl.UserServiceImpl;
import com.zhunaer.service.impl.OrderServiceImpl;
import com.zhunaer.service.impl.RoomServiceImpl;
import com.zhunaer.entity.Room;

public class ConsoleClient {
    /**
     * 用户服务接口，用于操作用户相关数据
     */
    private final UserService userService;

    /**
     * 订单服务接口，用于处理订单业务逻辑
     */
    private final OrderService orderService;

    /**
     * 房间服务接口，用于查询和管理房间信息
     */
    private final RoomService roomService;

    /**
     * 控制台输入扫描器，用于读取用户输入
     */
    private final Scanner scanner = new Scanner(System.in);

    /**
     * 构造控制台客户端实例
     *
     * @param userService  用户服务实现对象
     * @param orderService 订单服务实现对象
     * @param roomService  房间服务实现对象
     */
    public ConsoleClient(UserService userService, OrderService orderService, RoomService roomService) {
        this.userService = userService;
        this.orderService = orderService;
        this.roomService = roomService;
    }

    /**
     * 应用程序主入口
     *
     * @param args 命令行参数，当前版本未使用
     */
    public static void main(String[] args) {
        UserService userService = new UserServiceImpl();
        OrderService orderService = new OrderServiceImpl();
        RoomService roomService = new RoomServiceImpl();

        new ConsoleClient(userService, orderService, roomService).startBookingProcess();
    }

    /**
     * 启动酒店预订流程
     *
     * <p>该方法执行以下操作：
     * <ol>
     *   <li>提示用户输入必要信息（用户ID、房型ID、预订数量）</li>
     *   <li>验证输入数据的有效性</li>
     *   <li>调用服务层获取用户和房间信息</li>
     *   <li>创建新的订单记录</li>
     *   <li>处理可能出现的各种异常情况</li>
     * </ol>
     */
    public void startBookingProcess() {
        try {
            System.out.print("请输入用户ID：");
            int userId = Integer.parseInt(scanner.nextLine());

            System.out.print("请输入房型ID：");
            int roomId = Integer.parseInt(scanner.nextLine());

            System.out.print("请输入预订数量：");
            int quantity = Integer.parseInt(scanner.nextLine());

            User user = userService.getUserById(userId);
            Room room = roomService.getRoomById(roomId);

            orderService.createOrder(user, room, quantity);
            System.out.println("预订成功！");

        } catch (NumberFormatException e) {
            System.out.println("输入格式错误，请检查输入内容");
        } catch (IllegalArgumentException e) {
            System.out.println("请求参数错误: " + e.getMessage());
        } catch (RuntimeException e) {
            handleBusinessException(e);
        } catch (Exception e) {
            System.out.println("系统异常，请稍后再试");
        }
    }

    /**
     * 处理业务逻辑异常
     *
     * @param e 捕获的业务异常对象
     */
    private void handleBusinessException(RuntimeException e) {
        String message = e.getMessage();

        if (message.contains("库存不足")) {
            System.out.println("错误: 所选房型库存不足");
        } else if (message.contains("余额不足")) {
            System.out.println("错误: 用户余额不足");
        } else {
            System.out.println("预订失败: " + message);
        }
    }
}